package com.xgkj.diyiketang.util.xiangce;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Parcel;
import android.os.Parcelable;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created by windo on 16/4/13.
 */
public class PhotoImageInfo implements Parcelable,Comparable<PhotoImageInfo>{
    public int imageId;
    public String thumbnailPath;
    public String imagePath;
    private Bitmap bitmap;
    public boolean isSelected = false;
    private boolean isUploading = true;
    private Bitmap thumbBitmap;


    public static final Creator<PhotoImageInfo> CREATOR = new Creator<PhotoImageInfo>() {
        @Override
        public PhotoImageInfo createFromParcel(Parcel in) {
            PhotoImageInfo info = new PhotoImageInfo();
            info.imageId = in.readInt();
            info.thumbnailPath = in.readString();
            info.imagePath = in.readString();
            info.bitmap = in.readParcelable(Bitmap.class.getClassLoader());
            info.isSelected = in.readByte() != 0;
            info.isUploading = in.readByte() != 0;
            info.thumbBitmap = in.readParcelable(Bitmap.class.getClassLoader());
            return info;
        }

        @Override
        public PhotoImageInfo[] newArray(int size) {
            return new PhotoImageInfo[size];
        }
    };

    public boolean isUploading() {
        return isUploading;
    }

    public void setUploading(boolean uploading) {
        isUploading = uploading;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getThumbnailPath() {
        return thumbnailPath;
    }

    public void setThumbnailPath(String thumbnailPath) {
        this.thumbnailPath = thumbnailPath;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public Bitmap getBitmap() {
        if(bitmap == null){
            try {
                bitmap = revitionImageSize(imagePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public Bitmap getThumbBitmap() {
        if(thumbBitmap == null){
            try {
                thumbBitmap = getSmallImage(imagePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return thumbBitmap;
    }

    public void setThumbBitmap(Bitmap thumbBitmap) {
        this.thumbBitmap = thumbBitmap;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    public Bitmap revitionImageSize(String path) throws IOException {
        File file = new File(path);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(
                new File(path)));
        int jiaodu = readPictureDegree(file.getAbsolutePath());
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        int i = 0;
        Bitmap bitmap = null;
        while (true) {
            if ((options.outWidth >> i <= 1000)
                    && (options.outHeight >> i <= 1000)) {
                in = new BufferedInputStream(
                        new FileInputStream(new File(path)));
                options.inSampleSize = (int) Math.pow(2.0D, i);
                options.inJustDecodeBounds = false;
                if (jiaodu != 0) {
                    Bitmap midbitmap = BitmapFactory.decodeStream(in, null, options);
                    bitmap = rotaingImageView(jiaodu, midbitmap);
                    midbitmap.recycle();
                }else {
                    bitmap = BitmapFactory.decodeStream(in, null, options);
                }
//                bitmap = BitmapFactory.decodeStream(in, null, options);
                break;
            }
            i += 1;
        }
        return bitmap;
    }

    public Bitmap getSmallImage(String path) throws IOException {
        File file = new File(path);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        int jiaodu = readPictureDegree(file.getAbsolutePath());
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        int i = 0;
        Bitmap bitmap = null;
        while (true) {
            if ((options.outWidth >> i <= 256)
                    && (options.outHeight >> i <= 256)) {
                in = new BufferedInputStream(
                        new FileInputStream(new File(path)));
                options.inSampleSize = (int) Math.pow(2.0D, i);
                options.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(in, null, options);
                if (jiaodu != 0) {
                    Bitmap midbitmap = BitmapFactory.decodeStream(in, null, options);
                    bitmap = rotaingImageView(jiaodu, midbitmap);
                    midbitmap.recycle();
                }else {
                    bitmap = BitmapFactory.decodeStream(in, null, options);
                }
                break;
            }
            i += 1;
        }
        return bitmap;
    }

    public  Bitmap rotaingImageView(int angle , Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();;
        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.recycle();
        return resizedBitmap;
    }

    public  int readPictureDegree(String path) {
        int degree  = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    @Override
    public int compareTo(PhotoImageInfo another) {
        if (another ==null)return 0;
        int anotherId = another.getImageId();
        if (imageId>anotherId){
            return -1;
        }else if (imageId<anotherId){
            return 1;
        }

        return 0;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(imageId);
        dest.writeString(thumbnailPath);
        dest.writeString(imagePath);
        dest.writeParcelable(bitmap, flags);
        dest.writeByte((byte) (isSelected ? 1 : 0));
        dest.writeByte((byte) (isUploading ? 1 : 0));
        dest.writeParcelable(thumbBitmap, flags);
    }
}
